﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;

namespace WeakEndForum
{
    public partial class ForumPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection(
           ConfigurationManager.ConnectionStrings[Constantes.DB_NAME].ConnectionString);
            conn.Open();
            OleDbCommand comm = new OleDbCommand("SELECT ID, Titre, Auteur, Date_Creation, Ferme FROM Sujets ORDER BY Date_Creation DESC;", conn);
            
            OleDbDataReader dataReader = comm.ExecuteReader();

            TableHeaderRow headerRow = new TableHeaderRow();
            TableHeaderCell headSujet = new TableHeaderCell();
            headSujet.Text = "SUJET";
            TableHeaderCell headPar = new TableHeaderCell();
            headPar.Text = "PAR";
            TableHeaderCell headDate = new TableHeaderCell();
            headDate.Text = "DATE DE PUBLICATION";
            TableHeaderCell headStatut = new TableHeaderCell();
            headStatut.Text = "STATUT";
            headerRow.Cells.Add(headSujet);
            headerRow.Cells.Add(headPar);
            headerRow.Cells.Add(headDate);
            headerRow.Cells.Add(headStatut);
            tblForum.Rows.Add(headerRow);

            for (int i = 0; dataReader.Read(); i++)
            {
                TableRow row = new TableRow();
                TableCell topicCell = new TableCell();
                TableCell authorCell = new TableCell();
                TableCell dateCell = new TableCell();
                TableCell statusCell = new TableCell();
                HyperLink topicLink = new HyperLink();
                topicLink.NavigateUrl = "Topic.aspx?tID=" + ((int)dataReader[0]).ToString();
                topicLink.Text = (string)dataReader[1];
                topicCell.Controls.Add(topicLink);
                authorCell.Text = (string)dataReader[2];
                DateTime date = (DateTime)dataReader[3];
                if (!(bool)dataReader[4])
                {
                    statusCell.Text = "Ouvert";
                }
                else
                {
                    statusCell.Text = "Fermé";
                }
                dateCell.Text = date.ToString("d MMM yyyy");
                topicCell.CssClass = "forumTitleColumn";
                authorCell.CssClass = dateCell.CssClass = "forumOtherColumns";
                row.Cells.Add(topicCell);
                row.Cells.Add(authorCell);
                row.Cells.Add(dateCell);
                row.Cells.Add(statusCell);
                tblForum.Rows.Add(row);
            }
            conn.Close();
        }

        protected bool IsMod()
        {
            return Modules.isMod(Modules.getConnectedUser());
        }
    }
}